# Maintainer: Peter Budai <peterbudai at hotmail dot com>

_realname=pugixml
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.10
pkgrel=1
pkgdesc="Light-weight, simple and fast XML parser for C++ with XPath support (mingw-w64)"
arch=('any')
url="https://pugixml.org/"
license=("MIT")
depends=()
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-cmake")
source=(https://github.com/zeux/pugixml/releases/download/v${pkgver}/${_realname}-${pkgver}.tar.gz
        001-correct-lib-location.patch)
sha256sums=('55f399fbb470942410d348584dc953bcaec926415d3462f471ef350f29b5870a'
            '44df1ee3bea0476f0e06f565c7bb6a94ea8f5a60b29a274883f319d8cef699b5')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -p1 -i ${srcdir}/001-correct-lib-location.patch
}

build() {
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  [[ -d ${srcdir}/build-shared-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-shared-${MINGW_CHOST}
  mkdir -p ${srcdir}/build-shared-${MINGW_CHOST} && cd ${srcdir}/build-shared-${MINGW_CHOST}

  msg "Build shared library"
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    ${extra_config} \
    -DBUILD_SHARED_LIBS=ON \
    ../${_realname}-${pkgver}

  make
  
  [[ -d ${srcdir}/build-static-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-static-${MINGW_CHOST}
  mkdir -p ${srcdir}/build-static-${MINGW_CHOST} && cd ${srcdir}/build-static-${MINGW_CHOST}

  msg "Build static library"
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    ${extra_config} \
    -DBUILD_SHARED_LIBS=OFF \
    ../${_realname}-${pkgver}

  make
}

package() {
  msg "Install static library"
  cd "${srcdir}/build-static-${MINGW_CHOST}"
  make DESTDIR=${pkgdir} install

  msg "Install shared library"
  cd "${srcdir}/build-shared-${MINGW_CHOST}"
  make DESTDIR=${pkgdir} install
}
